package petrochina.company.electric.ui.dialog;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;

import petrochina.company.electric.R;
import petrochina.company.electric.aop.SingleClick;

/**
 * 任务人工确认弹框
 */
public final class TaskCustomerConfirmDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder>
            implements BaseAdapter.OnChildClickListener,
            View.OnLayoutChangeListener, Runnable {

        @SuppressWarnings("rawtypes")
        @Nullable
        private TaskCustomerConfirmDialog.OnListener mListener;
        private boolean mAutoDismiss = true;

        private final AppCompatImageView mCancelView;
        private final AppCompatButton mSearchView;
        private final AppCompatTextView mTvSearchTitle,mTvConfirmContent;

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.task_customer_confirm_dialog);
            setAnimStyle(BaseDialog.ANIM_BOTTOM);
            setHeight(getResources().getDisplayMetrics().heightPixels * 1 / 2);
            mCancelView = findViewById(R.id.iv_ble_search_closer);
            mTvSearchTitle = findViewById(R.id.tv_search_title);
            mSearchView = findViewById(R.id.btn_confirm);
            mTvConfirmContent = findViewById(R.id.tv_confirm_content);
            setOnClickListener(mCancelView, mSearchView);

            initData();
        }

        private void initData() {

        }

        @Override
        public TaskCustomerConfirmDialog.Builder setGravity(int gravity) {
            switch (gravity) {
                // 如果这个是在中间显示的
                case Gravity.CENTER:
                case Gravity.CENTER_VERTICAL:
                    // 不显示取消按钮
                    setCancel(null);
                    // 重新设置动画
                    setAnimStyle(BaseDialog.ANIM_SCALE);
                    break;
                default:
                    break;
            }
            return super.setGravity(gravity);
        }

        @Override
        public TaskCustomerConfirmDialog.Builder addOnDismissListener(@NonNull BaseDialog.OnDismissListener listener) {
            return super.addOnDismissListener(listener);
        }

        public TaskCustomerConfirmDialog.Builder setCancel(@StringRes int id) {
            return setCancel(getString(id));
        }

        public TaskCustomerConfirmDialog.Builder setCancel(CharSequence text) {
            //隐藏
//            mCancelView.setText(text);
            return this;
        }

        public TaskCustomerConfirmDialog.Builder setAutoDismiss(boolean dismiss) {
            mAutoDismiss = dismiss;
            return this;
        }

        @SuppressWarnings("rawtypes")
        public TaskCustomerConfirmDialog.Builder setListener(TaskCustomerConfirmDialog.OnListener listener) {
            mListener = listener;
            return this;
        }

        @SingleClick
        @Override
        public void onClick(View view) {
            if (mAutoDismiss) {
                dismiss();
            }

            if (view == mCancelView) {
                if (mListener == null) {
                    return;
                }
                mListener.onCancel(getDialog());
            }
            if (view == mSearchView) {
                if (mListener == null) {
                    return;
                }
                mListener.onSelected(getDialog(),0,null);
            }

        }

        /**
         * {@link View.OnLayoutChangeListener}
         */
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            // 这里一定要加延迟，如果不加在 Android 9.0 上面会导致 setLayoutParams 无效
            post(this);
        }

        @Override
        public void run() {
        }

        /**
         * 获取屏幕的高度
         */
        private int getScreenHeight() {
            Resources resources = getResources();
            DisplayMetrics outMetrics = resources.getDisplayMetrics();
            return outMetrics.heightPixels;
        }

        @Override
        public void onChildClick(RecyclerView recyclerView, View childView, int position) {
//            if (mAutoDismiss) {
//                dismiss();
//            }
//            if (mListener == null) {
//                return;
//            }
//            mListener.onSelected(getDialog(), position, mAdapter.getItem(position));
//            stopScanBleDevice();
        }
    }

    public interface OnListener<T> {

        /**
         * 选择条目时回调
         */
        void onSelected(BaseDialog dialog, int position, T t);

        /**
         * 点击取消时回调
         */
        default void onCancel(BaseDialog dialog) {
        }
    }
}